package com.yoo.ons.function.album.viewmodel

import android.graphics.drawable.Drawable
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import com.yoo.module_base.base.Activities
import com.yoo.module_base.utils.date.DateUtil
import com.yoo.ons.databinding.ItemPersonAlbumBinding
import com.yoo.ons.dialog.PictureViewDialogFragment
import com.yoo.ons.model.PictureManagerModel
import com.yoo.ons.util.appContext

class PersonAlbumViewViewHolder(private val binding: ItemPersonAlbumBinding) :
    RecyclerView.ViewHolder(binding.root) {
    fun bind(item: PictureManagerModel,position: Int) {
        val url = item.imageUrl
        if (url == "") {
            if (item.loading) {
                binding.itemProgress.visibility = View.VISIBLE
            } else {
                binding.itemProgress.visibility = View.GONE
                binding.itemImg.visibility = View.GONE
            }
        } else {
            binding.itemProgress.visibility = View.VISIBLE
            binding.itemImg.visibility = View.VISIBLE
            binding.itemImg.setOnClickListener {
//                val bundle = Bundle()
//                bundle.putStringArrayList(
//                    "images",
//                    arrayListOf(url)
//                )
//                bundle.putInt("index", 0)
//                bundle.putInt(
//                    "type",
//                    if (url.contains("mp4", true) || url.contains(
//                            "3gp",
//                            true
//                        ) || url.contains("mov", true)
//                    ) 1 else 0
//                )//0照片 1视频
//                bundle.putInt("position", position)
//                bundle.putString("imageCode", item.imageCode)
//                bundle.putString("albumCode", albumsCode)
//                if (url.contains("mp4", true) || url.contains("3gp", true) || url.contains(
//                        "mov",
//                        true
//                    )
//                ) {
//                    val intent = Intent(context, MyPictureVideoPlayActivity::class.java)
//                    val b = Bundle()
//                    b.putString("video_path", url)
//                    b.putInt("position", position)
//                    b.putString("imageCode", item.imageCode)
//                    b.putString("albumCode", albumsCode)
//                    intent.putExtras(b)
//                    context.startActivity(intent)
//                } else {
//                    IntentUtil.startActivity(LookImagesActivity::class.java, bundle)
//                }
                PictureViewDialogFragment.newInstance(item.imageCode,position).show(Activities.get().top?.supportFragmentManager)

            }

            if (url.contains("mp4", true) || url.contains("3gp", true) || url.contains(
                    "mov",
                    true
                )
            ) {
                binding.videoTimes.visibility = View.VISIBLE
                binding.videoTimes.text = item.videoLength?.let {
                    DateUtil.timeConversion(
                        it / 1000
                    )
                }
            } else {
                binding.videoTimes.visibility = View.GONE
            }
            Glide.with(appContext)
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .listener(object : RequestListener<Drawable> {
                    override fun onLoadFailed(
                        e: GlideException?,
                        model: Any?,
                        target: Target<Drawable>?,
                        isFirstResource: Boolean
                    ): Boolean {
                        binding.itemProgress.visibility = View.GONE
                        return false
                    }

                    override fun onResourceReady(
                        resource: Drawable?,
                        model: Any?,
                        target: Target<Drawable>?,
                        dataSource: DataSource?,
                        isFirstResource: Boolean
                    ): Boolean {
                        binding.itemProgress.visibility = View.GONE
                        return false
                    }

                })
                .into(binding.itemImg)
        }
    }
}